#include<windows.h>
#include<iostream>
#include<random>

using namespace std;


char letters[26*2];

random_device rd;
mt19937 gen(rd());

// 生成一个 0 到 26*2-1 之间的随机整数
uniform_int_distribution<int> dis(0, 26*2-1);
//int randomNumber = dis(gen);

void init_letters(){
	
	for(int i=0;i<26;i++){
		letters[i] = 'a' + i;
		letters[i+26] = 'A' + i;
	}
	
	
	
}

char random_char(){
	return letters[dis(gen)];
}


int main(){

	init_letters();
	for(int i=0;i<26*2;i++){
		cout << letters[i];
	}
	cout << endl;
	
	HANDLE hFile = CreateFile("share_memory_file.txt",
								GENERIC_ALL,
								0,
								NULL,
								OPEN_ALWAYS,
								FILE_ATTRIBUTE_NORMAL,
								NULL
								);
								
	if(hFile == INVALID_HANDLE_VALUE){
		cerr << "CreateFile failed. Error: "<< GetLastError() << endl;
		
		return 1;
	}							
								
	HANDLE hMap = CreateFileMapping(hFile,
									NULL,
									PAGE_EXECUTE_READWRITE,
									0,
									1024,
									NULL
									);
	
	if(!hMap){
		cerr << "CreateFileMapping failed. Error: "<< GetLastError() << endl;
		CloseHandle(hFile);
		return 1;
	}
	
	LPVOID pView = MapViewOfFile(hMap,
								FILE_MAP_ALL_ACCESS|FILE_MAP_EXECUTE,
								0,
								0,
								1024
								);
								
	if(!pView){
		cerr << "MapViewOfFile failed. Error: " << GetLastError() << endl;
		CloseHandle(hFile);
		CloseHandle(hMap);
		return 1;
	}						
	
	
	unsigned char *p = (unsigned char*)pView;
	
	for(int i=0;i<1024;i++){
		//*(p+i) = random_char();
		p[i] = random_char();
	}
	//越界会有问题
	//p[10000] = 'A';
	//cout << p[10000] << endl;
	
	getchar();
	
	
	UnmapViewOfFile(pView);
	CloseHandle(hMap);
	CloseHandle(hFile);
	
	
	
	cout << "Over!" << endl;
								
								
								
	return 0;							
								
								
	
}